package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptTree;
import com.ruoyi.system.domain.ZhuanjiaQushiqueren;
import com.ruoyi.system.service.IZhuanjiaQushiquerenService;
import com.ruoyi.system.tools.BooleanMessage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeptMapper;
import com.ruoyi.system.domain.Dept;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.common.core.text.Convert;

/**
 * 单位信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-08
 */
@Service
public class DeptServiceImpl implements IDeptService 
{
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private IZhuanjiaQushiquerenService zhuanjiaQushiquerenService;

    /**
     * 查询单位信息
     * 
     * @param id 单位信息主键
     * @return 单位信息
     */
    @Override
    public Dept selectDeptById(String id)
    {
        return deptMapper.selectDeptById(id);
    }

    /**
     * 查询单位信息列表
     * 
     * @param dept 单位信息
     * @return 单位信息
     */
    @Override
    public List<Dept> selectDeptList(Dept dept)
    {
        return deptMapper.selectDeptList(dept);
    }

    /**
     * 新增单位信息
     * 
     * @param dept 单位信息
     * @return 结果
     */
    @Override
    public int insertDept(Dept dept)
    {
        return deptMapper.insertDept(dept);
    }

    /**
     * 修改单位信息
     * 
     * @param dept 单位信息
     * @return 结果
     */
    @Override
    public int updateDept(Dept dept)
    {
        return deptMapper.updateDept(dept);
    }

    /**
     * 批量删除单位信息
     * 
     * @param ids 需要删除的单位信息主键
     * @return 结果
     */
    @Override
    public int deleteDeptByIds(String ids)
    {
        return deptMapper.deleteDeptByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单位信息信息
     * 
     * @param id 单位信息主键
     * @return 结果
     */
    @Override
    public int deleteDeptById(String id)
    {
        return deptMapper.deleteDeptById(id);
    }

    /**
     * 查询单位信息列表custom
     *
     * @param dept 单位信息
     * @return 单位信息
     */
    @Override
    public List<Map<String,Object>> selectDeptListCustom(Dept dept)
    {
        dept.setJigoumingcheng("%"+dept.getJigoumingcheng()+"%");
        return deptMapper.selectDeptListCustom(dept);
    }

    /**
     * @return List<DeptTree>
     */
    @Override
    public List<DeptTree> selectDeptTreeList() {
        List<Dept> deptList = deptMapper.selectDeptTreeList();
        List<DeptTree> deptTrees = initDeptTree(deptList);
        return deptTrees;
    }
    /**
     * @return List<DeptTree>
     */
    @Override
    public List<DeptTree> XiaoxideptTreeList(String olddeptid,String parentid) {
        List<Dept> deptList = deptMapper.XiaoxideptTreeList(olddeptid,parentid);
        List<DeptTree> deptTrees = initDeptTree(deptList);
        return deptTrees;
    }

    /**
     * 对象转单位树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<DeptTree> initDeptTree(List<Dept> deptList)
    {

        List<DeptTree> deptTrees = new ArrayList<DeptTree>();
        for (Dept dept : deptList)
        {
            DeptTree deptTree = new DeptTree();
            deptTree.setId(dept.getId());
            deptTree.setpId(dept.getParentid());
            deptTree.setName(dept.getJigoumingcheng());
            deptTree.setTitle(dept.getJigoumingcheng());
            deptTree.setJigoujibie(dept.getJigoujibie());
            deptTrees.add(deptTree);
        }


        return deptTrees;
    }

    @Override
    public Dept findJibie1(String ppDeptId){
        Dept dept = deptMapper.selectDeptById(ppDeptId);
        String mmParentId = dept.getParentid();

        Dept mmDept = deptMapper.selectDeptById(mmParentId);
        String mmJigoujibie = mmDept.getJigoujibie();
        if (mmJigoujibie.equals("1")){
            return mmDept;
        }
        return findJibie1(mmParentId);
    }

    @Override
    public Dept findJibie2(String ppDeptId){
        Dept dept = deptMapper.selectDeptById(ppDeptId);
        String mmParentId = dept.getParentid();

        Dept mmDept = deptMapper.selectDeptById(mmParentId);
        String mmJigoujibie = mmDept.getJigoujibie();
        if (mmJigoujibie.equals("2")){
            return mmDept;
        }
        return findJibie2(mmParentId);
    }

    @Override
    public List<String> findToZJZX(String ppDeptId,List<String> ppDeptidList){
        Dept dept = deptMapper.selectDeptById(ppDeptId);
        String mmParentId = dept.getParentid();

        Dept mmDept = deptMapper.selectDeptById(mmParentId);
        String mmDeptId = mmDept.getId();

        ppDeptidList.add(mmDeptId);

        if (mmDeptId.equals(RuoYiConfig.getZjzxolddeptid())){
            return ppDeptidList;
        }
        return findToZJZX(mmDeptId,ppDeptidList);
    }

    @Override
    public List<Dept> findCengjiAll(String ppDeptId){
        Dept mmDept = new Dept();
        mmDept.setParentid(ppDeptId);
        mmDept.setIsvalid((long)1);
        List<Dept> mmDeptList = selectDeptList(mmDept);
        List<Dept> mmXiajiDeptList = findCengji(mmDeptList,0);
        return mmXiajiDeptList;
    }

    /**
     * 单位拨款详情 查询单位list
     *
     * @param map
     * @return 单位list
     */
    @Override
    public List<Map> selectJingfeiDeptList(Map<String, Object> map) {
        return deptMapper.selectJingfeiDeptList(map);
    }

    /**
     * 检查单位必要信息是否填写
     *
     * @param userId
     * @return Boolean
     */
    @Override
    public BooleanMessage checkDeptInfo(Long userId) {
        boolean ok = true;
        Dept d = deptMapper.checkDeptInfo(userId);

        //专家中心不判断
        if(d.getId().equals("000"))return BooleanMessage.getSuccessMessage("");

        //经办人，经办人办公电话，经办人手机号码，开户行，开户名称，银行账号，财务联系人，财务联系人办公电话，财务联系人手机号码
        if(d.getKaihuhang().isEmpty()||d.getKaihumingcheng().isEmpty()||d.getYinhangzhanghao().isEmpty()||
                d.getJingbanren().isEmpty()||d.getJingbanrenbangongdianhua().isEmpty()||d.getJingbanrenshoujihaoma().isEmpty()||
                d.getCaiwulianxiren().isEmpty()||d.getCaiwubangongdianhua().isEmpty()||d.getCaiwushoujihaoma().isEmpty()){
             return BooleanMessage.getErrorMessage("请完善单位相关信息");
        }

/*        ZhuanjiaQushiqueren zhuanjiaQushiqueren = new ZhuanjiaQushiqueren();
        zhuanjiaQushiqueren.setJigoudaima(d.getJigoudaima());
        zhuanjiaQushiqueren.setSiwangbiaoji("确定死亡");
        zhuanjiaQushiqueren.setShifouchuli(0l);
        List<ZhuanjiaQushiqueren> list = zhuanjiaQushiquerenService.selectZhuanjiaQushiquerenList(zhuanjiaQushiqueren);
        if(list.size()>0){
            return BooleanMessage.getWarnMessage("有去世待核查专家，请先处理。");
        }*/


        return BooleanMessage.getSuccessMessage("");
    }

    /**
     * @return
     */
    @Override
    public List<Map<String, Object>> bindQushiquerenDeptList(String deptid,String danweimingcheng) {
        return deptMapper.bindQushiquerenDeptList(deptid,danweimingcheng);
    }

    /**
     * @return
     */
    @Override
    public List<Map<String, Object>> bindQushiquerenDeptListself(String deptid,String danweimingcheng) {
        return deptMapper.bindQushiquerenDeptListself(deptid,danweimingcheng);
    }

    /**
     * @param deptid
     * @return
     */
    @Override
    public List<Map<String, Object>> selectZhanghaoxinxi(String deptid) {
        String ZJZXOldDeptId = RuoYiConfig.getZjzxolddeptid();
        return deptMapper.selectZhanghaoxinxi(deptid,ZJZXOldDeptId);
    }

    public List<Dept> findCengji(List<Dept> ppDeptList,int ppLunci){
        List<Dept> mmDeptList = new ArrayList<>();
        String ppPingjie = "";
        for (int a=0;a<ppLunci;a++){
            ppPingjie = ppPingjie + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        for (int i=0;i<ppDeptList.size();i++){
            ppDeptList.get(i).setJigoumingcheng(ppPingjie+ppDeptList.get(i).getJigoumingcheng());
            mmDeptList.add(ppDeptList.get(i));

            Dept mmDept = new Dept();
            mmDept.setParentid(ppDeptList.get(i).getId());
            mmDept.setIsvalid((long)1);
            List<Dept> mmXiajiDeptList = selectDeptList(mmDept);
            if (mmXiajiDeptList.size()>0){
                List<Dept> mmXiashuList = findCengji(mmXiajiDeptList,ppLunci+1);
                mmDeptList.addAll(mmXiashuList);
            }
        }

        return mmDeptList;
    }

    @Override
    public List<Map<String,Object>> searchDiqu(String diqu){
        diqu = "%"+diqu+"%";
        return deptMapper.searchDiqu(diqu);
    }

    /**
     * @param jigoumingcheng
     * @return
     */
    @Override
    public List<Dept> deptsearch(String jigoumingcheng) {
        return deptMapper.deptsearch(jigoumingcheng);
    }

}
